package com.dtz.test;

import org.eclipse.jgit.api.TransportConfigCallback;
import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig;
import org.eclipse.jgit.transport.SshSessionFactory;
import org.eclipse.jgit.transport.SshTransport;
import org.eclipse.jgit.transport.Transport;
import org.eclipse.jgit.util.FS;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class SshTransportConfigCallback implements TransportConfigCallback {

    private final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
        @Override
        protected void configure(OpenSshConfig.Host hc, Session session) {
            session.setConfig("StrictHostKeyChecking","no");
        }

        @Override
        protected JSch createDefaultJSch(FS fs) throws JSchException {
        	// 生成key -》 ssh-keygen -m PEM -r rsa -C "your email"
        	String prKey = "";	// 添加private key内容
            JSch jSch = super.createDefaultJSch(fs);
            jSch.addIdentity("ssh", prKey.getBytes(), null, null);
            //jSch.addIdentity("E:\\id_rsa", "");
            return jSch;
        }
    };

    @Override
    public void configure(Transport transport) {
        SshTransport sshTransport = (SshTransport) transport;
        sshTransport.setSshSessionFactory(sshSessionFactory);
    }

}